package com.emm.yixun.website.controller.customer;
//框架包
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
//common
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.model.User;
//website
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;

//变化的引用
import com.emm.yixun.common.model.QuestionnaireQuestion;
import com.emm.yixun.common.model.QuestionnaireQuestionInfo;
import com.emm.yixun.common.model.dto.QuestionnaireQuestionDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.model.enums.IsAvlidEnum;
import com.emm.yixun.customer.service.QuestionnaireQuestionServiceFacade;


@Controller
@RequestMapping("/customer/questionnaireQuestion/")
public class QuestionnaireQuestionController extends BaseController<QuestionnaireQuestion>{
	
	@Autowired
	private QuestionnaireQuestionServiceFacade questionnaireQuestionServiceFacade;
	
	@RequestMapping(value="index")
	public String index(ModelMap map){
		if(doSecurityIntercept(Const.RESOURCES_TYPE_MENU)){
			//查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));		
			return "/customer/questionnaireQuestion/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}
	
	//新增返回实体对象
	@RequestMapping(value="add", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  add(QuestionnaireQuestionDto dto){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:add"))){
        	try {
        		User user=ShiroUtil.getCurrentUser();
        		dto.setMerchantId(user.getMerchantId());
        		dto.setCreateUser(user.getId());
        		dto.setDelFlag("0");
        		dto.setUpdateUser(user.getId());
        		dto.setUpdateTime(new Date());
				Response<QuestionnaireQuestionDto> response=questionnaireQuestionServiceFacade.save(dto);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				}else{
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	}
    	return ar;
    }
	//查看
	@RequestMapping(value="view", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  view(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:view"))){
        	try {
				Response<QuestionnaireQuestion> response=questionnaireQuestionServiceFacade.findById(id);
				if(response.isSuccess()){
					ar.setSucceed(response.getEntity());
				}else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	
	//修改
	@RequestMapping(value="edit", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  edit(QuestionnaireQuestionDto dto){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用修改接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:edit"))){
        	try {
        		Response<Integer> response=questionnaireQuestionServiceFacade.updateByPrimaryKey(dto, true);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				}else{
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
    	}
    	return ar;
    }
	
	//删除
	@RequestMapping(value="delete", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  delete(Long id){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用查看接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:delete"))){
        	try {
        		Response<Integer> response=questionnaireQuestionServiceFacade.deleteByPrimaryKey(id);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				}else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
    	}
    	return ar;
    }
	
	
	
	//分页查询
	@RequestMapping(value="list", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes  list(QuestionnaireQuestionDto dto,Integer pageSize,Integer pageNum){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用分页查询接口
    	 *3.返回信息
    	*/
        if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:list"))){
        	try {
        		Page<QuestionnaireQuestion> response=questionnaireQuestionServiceFacade.findPageByDto(dto, PageUtil.begin(pageNum, pageSize),pageSize);
                if(response.getSuccess()){
                    if(null==response.getRows())response.setRows(new ArrayList<QuestionnaireQuestion>());
                	response.setPageNumber(pageNum);
                	ar.setSucceed(response);
                }else{
                	ar.setFailMsg(Const.DATA_FAIL);
                }
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	}
    	return ar;
    }
	
	//新增返回实体对象
	@RequestMapping(value="addOrUpdate", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes addOrUpdate(HttpServletRequest request){
    	AjaxRes ar=getAjaxRes();
    	
    	/*
    	 *1.判断权限 
    	 *2.调用新增接口
    	 *3.返回信息
    	*/
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:add"))){
        	try {
        		//模板主表ID
        		String modelId = request.getParameter("modelId");
        		if(StringUtils.isBlank(modelId)){
        			ar.setFailMsg(Const.SAVE_FAIL);
        			return ar;
        		}
        		//获取问卷内容
        		String arrayJson = request.getParameter("json");
        		if(StringUtils.isBlank(arrayJson)){
        			ar.setFailMsg(Const.SAVE_FAIL);
        			return ar;
        		}
        		//问卷问题表ID（不为空时表示修改问卷）
        		String questionId = request.getParameter("questionId");
        		User user=ShiroUtil.getCurrentUser();
        		QuestionnaireQuestionInfo dto = new QuestionnaireQuestionInfo();
        		dto.setQuestionnaireModelId(Long.parseLong(modelId));
        		dto.setArrayJson(arrayJson); 
        		dto.setMerchantId(user.getMerchantId());
    			dto.setCreateUser(user.getId());
    			dto.setCreateTime(new Date());
    			//判断是否为修改，questionId为空为新增
    			if(StringUtils.isNotBlank(questionId)){
    				dto.setId(Long.parseLong(questionId));
    			}
				Response<String> response=questionnaireQuestionServiceFacade
						.saveQuestionnaire(dto);
				if(response.isSuccess()){
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				}else{
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
    	//}
    	return ar;
    }
	
	//问卷修改-查询
	@RequestMapping(value="questionList", method=RequestMethod.POST)
	@ResponseBody
    public AjaxRes questionList(HttpServletRequest request){
    	AjaxRes ar=getAjaxRes();
    	/*
    	 *1.判断权限 
    	 *2.调用分页查询接口
    	 *3.返回信息
    	*/
        //if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"customer:questionnaireQuestion:list"))){
        	try {
        		String modelId = request.getParameter("questionnaireId");
        		String level = request.getParameter("level");
        		String problemCode = request.getParameter("problemCode");
        		
        		QuestionnaireQuestionDto dto = new QuestionnaireQuestionDto();
        		dto.setQuestionnaireModelId(Long.parseLong(modelId));
        		//问题等级
        		if(StringUtils.isNotBlank(level)){
        			dto.setLevel(Long.parseLong(level));
        		}
        		//问题编号
        		if(StringUtils.isNotBlank(problemCode)){
        			dto.setProblemCode(problemCode);
        		}
        		dto.setIsValid(IsAvlidEnum.AVILD.getNo());
    			dto.setDelFlag(DelFlagEnum.UNDEL.getNo());
    			dto.addAsc("sort");
        		ResponseList<QuestionnaireQuestionInfo> response = questionnaireQuestionServiceFacade.questionList(dto);
        		if(response.isSuccess()){
                    if(null==response.getEntity())response.setEntity(new ArrayList<QuestionnaireQuestionInfo>());
                	ar.setSucceed(response);
                }else{
                	ar.setFailMsg(Const.DATA_FAIL);
                }
        		
			} catch (Exception e) {
				logger.error(e.toString(),e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
    	//}
    	return ar;
    }
}
